﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{
    // NOTE: If you change the class name "Pensioner" here, you must also update the reference to "Pensioner" in App.config.
    public partial class MainService : IPensioner
    {
        public DataSet Pensioner_GetAll()
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Pensioner_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public DataSet Pensioner_DeNghiTroCap_OmDau(int BranchID, int month, int year, int grantid, int subgrantid)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Pensioner_DeNghiTroCap_OmDau", dbConn);
            dbCmd.Parameters.AddWithValue("@BranchID", BranchID);
            dbCmd.Parameters.AddWithValue("@month", month);
            dbCmd.Parameters.AddWithValue("@year", year);
            dbCmd.Parameters.AddWithValue("@grantid", grantid);
            dbCmd.Parameters.AddWithValue("@subgrantid", subgrantid);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public DataSet Pensioner_DeNghiTroCap_ThaiSan(int BranchID, int month, int year, int grantid, int subgrantid)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Pensioner_DeNghiTroCap_ThaiSan", dbConn);
            dbCmd.Parameters.AddWithValue("@BranchID", BranchID);
            dbCmd.Parameters.AddWithValue("@month", month);
            dbCmd.Parameters.AddWithValue("@year", year);
            dbCmd.Parameters.AddWithValue("@grantid", grantid);
            dbCmd.Parameters.AddWithValue("@subgrantid", subgrantid);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public DataSet Pensioner_DeNghiTroCap_SauOmDau(int BranchID, int month, int year)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Pensioner_DeNghiTroCap_SauOmDau", dbConn);
            dbCmd.Parameters.AddWithValue("@BranchID", BranchID);
            dbCmd.Parameters.AddWithValue("@month", month);
            dbCmd.Parameters.AddWithValue("@year", year);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public DataSet Pensioner_DeNghiTroCap_SauThaiSan(int BranchID, int month, int year)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Pensioner_DeNghiTroCap_SauThaiSan", dbConn);
            dbCmd.Parameters.AddWithValue("@BranchID", BranchID);
            dbCmd.Parameters.AddWithValue("@month", month);
            dbCmd.Parameters.AddWithValue("@year", year);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public DataSet Pensioner_DeNghiTroCap_SauTNLDBNN(int BranchID, int month, int year)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Pensioner_DeNghiTroCap_SauTNLDBNN", dbConn);
            dbCmd.Parameters.AddWithValue("@BranchID", BranchID);
            dbCmd.Parameters.AddWithValue("@month", month);
            dbCmd.Parameters.AddWithValue("@year", year);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public DataSet Pensioner_GetList(out int ItemCount, int currentPage, int pageSize, int branch_ID, int DepartmentID, int month, int year)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Pensioner_GetList", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            dbCmd.Parameters.AddWithValue("@BranchID", branch_ID);
            dbCmd.Parameters.AddWithValue("@DepartmentID", DepartmentID);
            dbCmd.Parameters.AddWithValue("@month", month);
            dbCmd.Parameters.AddWithValue("@year", year);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public PensionerInfo Pensioner_GetInfo(int PensionerID)
        {
            PensionerInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Pensioner_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@PensionerID", PensionerID);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new PensionerInfo();
                    retVal.ID = Convert.ToInt32(dr["Pensioner_ID"]);
                    retVal.GrantID = Convert.ToInt32(dr["Pensioner_GrantID"]);
                    retVal.SubGrantID = Convert.ToInt32(dr["Pensioner_SubGrantID"]);
                    retVal.EmployeeID = Convert.ToInt32(dr["Pensioner_EmployeeID"]);
                    retVal.BranchID = Convert.ToInt32(dr["Pensioner_BranchID"]);
                    retVal.DepartmentID = Convert.ToInt32(dr["Pensioner_DepartmentID"]);
                    retVal.AtHome = Convert.ToInt32(dr["Pensioner_AtHome"]);
                    retVal.Concentrate = Convert.ToInt32(dr["Pensioner_COncentrate"]);
                    retVal.InTime = Convert.ToInt32(dr["Pensioner_InTime"]);
                    retVal.Inherit = Convert.ToInt32(dr["Pensioner_Inherit"]);
                    retVal.Amount = Convert.ToDecimal(dr["Pensioner_Amount"].ToString());
                    retVal.Description = dr["Pensioner_Description"].ToString();
                    retVal.Date = Convert.ToDateTime(dr["Pensioner_Date"]);
                    retVal.DeclineLevel = Convert.ToDecimal(dr["Pensioner_DeclineLevel"]);
                    retVal.ConditiontoWork = dr["Pensioner_ConditiontoWork"].ToString();
                    retVal.InheritFoetus = Convert.ToInt32(dr["Pensioner_InheritForFoetusDepraved"]);
                    retVal.StateParturition = dr["Pensioner_StateParturition"].ToString();
                    retVal.InsuranceTime = dr["Pensioner_InsuranceTime"].ToString();
                    retVal.Salary = Convert.ToDecimal(dr["Pensioner_Salary"]);
                    retVal.BasicSalary = Convert.ToDecimal(dr["Pensioner_BasicSalary"]);
                    retVal.AVGAmount = Convert.ToDecimal(dr["Pensioner_AVGAmount"]);
                    retVal.Rate = Convert.ToDecimal(dr["Pensioner_Rate"]);
                    retVal.FromDate = dr["Pensioner_FromDate"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["Pensioner_FromDate"]);
                    retVal.ToDate = dr["Pensioner_ToDate"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["Pensioner_ToDate"]);

                    retVal.ChildNumber = dr["Pensioner_ChildNumber"] == DBNull.Value ? 0 : Convert.ToInt32(dr["Pensioner_ChildNumber"]);
                    retVal.DateOfBirth = dr["Pensioner_DateOfBirth"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["Pensioner_DateOfBirth"]);
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public int Pensioner_Insert(PensionerInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Pensioner_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Pensioner_GrantID", eInfo.GrantID);
            dbCmd.Parameters.AddWithValue("@Pensioner_SubGrantID", eInfo.SubGrantID);
            dbCmd.Parameters.AddWithValue("@Pensioner_EmployeeID", eInfo.EmployeeID);
            dbCmd.Parameters.AddWithValue("@Pensioner_DepartmentID", eInfo.DepartmentID);
            dbCmd.Parameters.AddWithValue("@Pensioner_InTime", eInfo.InTime);
            dbCmd.Parameters.AddWithValue("@Pensioner_Inherit", eInfo.Inherit);
            dbCmd.Parameters.AddWithValue("@Pensioner_AtHome", eInfo.AtHome);
            dbCmd.Parameters.AddWithValue("@Pensioner_Concentrate", eInfo.Concentrate);
            dbCmd.Parameters.AddWithValue("@Pensioner_Amount", eInfo.Amount);
            dbCmd.Parameters.AddWithValue("@Pensioner_Description", eInfo.Description);
            if (eInfo.Date == DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@Pensioner_Date", DBNull.Value);
            else
                dbCmd.Parameters.AddWithValue("@Pensioner_Date", eInfo.Date);
            dbCmd.Parameters.AddWithValue("@Pensioner_BranchID", eInfo.BranchID);
            dbCmd.Parameters.AddWithValue("@Pensioner_DeclineLevel", eInfo.DeclineLevel);
            dbCmd.Parameters.AddWithValue("@Pensioner_StateParturition", eInfo.StateParturition);
            dbCmd.Parameters.AddWithValue("@Pensioner_InheritForFoetusDepraved", eInfo.InheritFoetus);
            dbCmd.Parameters.AddWithValue("@Pensioner_ConditiontoWork", eInfo.ConditiontoWork);
            dbCmd.Parameters.AddWithValue("@Pensioner_InsuranceTime", eInfo.InsuranceTime);
            dbCmd.Parameters.AddWithValue("@Pensioner_Salary", eInfo.Salary);
            if (eInfo.FromDate == DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@Pensioner_FromDate", DBNull.Value);
            else
                dbCmd.Parameters.AddWithValue("@Pensioner_FromDate", eInfo.FromDate);
            if (eInfo.ToDate == DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@Pensioner_ToDate", DBNull.Value);
            else
                dbCmd.Parameters.AddWithValue("@Pensioner_ToDate", eInfo.ToDate);
            dbCmd.Parameters.AddWithValue("@Pensioner_BasicSalary", eInfo.BasicSalary);
            dbCmd.Parameters.AddWithValue("@Pensioner_AVGAmount", eInfo.AVGAmount);
            dbCmd.Parameters.AddWithValue("@Pensioner_Rate", eInfo.Rate);

            dbCmd.Parameters.AddWithValue("@Pensioner_ChildNumber", eInfo.ChildNumber);

            if (eInfo.Date == DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@Pensioner_DateOfBirth", DBNull.Value);
            else
                dbCmd.Parameters.AddWithValue("@Pensioner_DateOfBirth", eInfo.Date);


            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool Pensioner_Update(PensionerInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Pensioner_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Pensioner_ID", eInfo.ID);
            dbCmd.Parameters.AddWithValue("@Pensioner_GrantID", eInfo.GrantID);
            dbCmd.Parameters.AddWithValue("@Pensioner_SubGrantID", eInfo.SubGrantID);
            dbCmd.Parameters.AddWithValue("@Pensioner_EmployeeID", eInfo.EmployeeID);
            dbCmd.Parameters.AddWithValue("@Pensioner_AtHome", eInfo.AtHome);
            dbCmd.Parameters.AddWithValue("@Pensioner_Concentrate", eInfo.Concentrate);
            dbCmd.Parameters.AddWithValue("@Pensioner_InTime", eInfo.InTime);
            dbCmd.Parameters.AddWithValue("@Pensioner_Inherit", eInfo.Inherit);
            dbCmd.Parameters.AddWithValue("@Pensioner_Amount", eInfo.Amount);
            dbCmd.Parameters.AddWithValue("@Pensioner_Description", eInfo.Description);
            if (eInfo.Date == DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@Pensioner_Date", DBNull.Value);
            else
                dbCmd.Parameters.AddWithValue("@Pensioner_Date", eInfo.Date);
            dbCmd.Parameters.AddWithValue("@Pensioner_DepartmentID", eInfo.DepartmentID);
            dbCmd.Parameters.AddWithValue("@Pensioner_BranchID", eInfo.BranchID);
            dbCmd.Parameters.AddWithValue("@Pensioner_DeclineLevel", eInfo.DeclineLevel);
            dbCmd.Parameters.AddWithValue("@Pensioner_StateParturition", eInfo.StateParturition);
            dbCmd.Parameters.AddWithValue("@Pensioner_InheritForFoetusDepraved", eInfo.InheritFoetus);
            dbCmd.Parameters.AddWithValue("@Pensioner_ConditiontoWork", eInfo.ConditiontoWork);
            dbCmd.Parameters.AddWithValue("@Pensioner_InsuranceTime", eInfo.InsuranceTime);
            dbCmd.Parameters.AddWithValue("@Pensioner_Salary", eInfo.Salary);
            if (eInfo.FromDate == DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@Pensioner_FromDate", DBNull.Value);
            else
                dbCmd.Parameters.AddWithValue("@Pensioner_FromDate", eInfo.FromDate);
            if (eInfo.ToDate == DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@Pensioner_ToDate", DBNull.Value);
            else
                dbCmd.Parameters.AddWithValue("@Pensioner_ToDate", eInfo.ToDate);
            dbCmd.Parameters.AddWithValue("@Pensioner_BasicSalary", eInfo.BasicSalary);
            dbCmd.Parameters.AddWithValue("@Pensioner_AVGAmount", eInfo.AVGAmount);
            dbCmd.Parameters.AddWithValue("@Pensioner_Rate", eInfo.Rate);

            dbCmd.Parameters.AddWithValue("@Pensioner_ChildNumber", eInfo.ChildNumber);
            dbCmd.Parameters.AddWithValue("@Pensioner_DateOfBirth", eInfo.DateOfBirth);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool Pensioner_Delete(int PensionerID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_PENSIONER_DELETE", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@PENSIONERID", PensionerID);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}


